"------------------------------------------------------------
"Vim config file of Edgar Wu(edgar.wu@gmail.com)
"------------------------------------------------------------
set nocompatible

"use windows style action
source $VIMRUNTIME/mswin.vim

"behave
behave mswin

"enable file type plugin
filetype on
filetype plugin on
filetype plugin indent on

"set mapleader
let mapleader = ","
let g:mapleader = ","

"Set to auto read when a file is changed from the outside
set autoread

"backup
set nobackup
set nowb
set noswapfile

"ruler
set	noeb novb ruler

"backspace
set backspace=indent,eol,start whichwrap+=<,>,[,]

"encoding
set fileencodings=ucs-bom,UTF-8,GBK,BIG5,latin1

"search
set incsearch
set hlsearch
set ignorecase

"command
set showcmd
set showmatch

"tab setting
set smarttab
set tabstop=4
set softtabstop=4
set shiftwidth=4

"show line number
set number

"highlight current line
set cursorline

"indent setting
set autoindent
set smartindent 

"start window
"autocmd GUIEnter * simalt ~x
winsize 120 35
winpos 10 20

"disable fold by default
set foldlevel=99

"color and fonts
syntax on
if has("gui_running")
	colorscheme textmate
	"tango2 darkocean textmate oceandeep
	"hide tool bar
	set guioptions-=T
else 
	" this one looks decent in terminals
	colorscheme tango2
	" popup menu
	highlight   Pmenu               term=NONE cterm=NONE ctermfg=7 ctermbg=5 gui=NONE guifg=White guibg=Magenta
	highlight   PmenuSel            term=NONE cterm=NONE ctermfg=0 ctermbg=7 gui=NONE guifg=Black guibg=White
	highlight   PmenuSbar           term=NONE cterm=NONE ctermfg=7 ctermbg=0 gui=NONE guifg=White guibg=Black
	highlight   PmenuThumb			term=NONE cterm=NONE ctermfg=0 ctermbg=7 gui=NONE guifg=Black guibg=White
	highlight   FoldColumn			ctermbg=Black ctermfg=Gray
endif

"------------------------------------------------------------
"plugin settings
"------------------------------------------------------------
"CTag and omni-complete
map <C-F12> :!ctags -R .<CR>
set autochdir
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD", "boost"]

"Minibufexpl
"let g:miniBufExplModSelTarget = 1
"let g:miniBufExplorerMoreThanOne = 2
"let g:miniBufExplModSelTarget = 0
"let g:miniBufExplUseSingleClick = 1
"let g:miniBufExplMapWindowNavVim = 1
"let g:miniBufExplVSplit = 25
"let g:miniBufExplSplitBelow=1

"TagList
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
nmap tl :TlistToggle<CR>

"NERD Tree
nmap nl :NERDTreeToggle<CR>

"window manager
let g:winManagerWindowLayout='FileExplorer|TagList'
nmap wm :WMToggle<CR>

"snippets Emu
"let g:snippetsEmu_key = "<A-1>"

"XML plugin
let xml_use_xhtml = 1

"------------------------------------------------------------
"Key mapping
"------------------------------------------------------------
"Fast saving
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>

"Smart way to move btw. windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

"nmap <F2> :bprevious<CR>
nmap <F3> <C-W>w
nmap <F5> :call Debug()<CR>
nmap <F6> :call Comp()<CR>
nmap <F7> :call Run()<CR>
nmap <F8> :call ToHtml()<CR>
nmap <F12> :make<CR>

"imap <F2> <ESC>:bprevious<CR>
imap <F3> <C-W>w
imap <F5> <ESC>:call Debug()<CR>
imap <F6> <ESC>:call Comp()<CR> 
imap <F7> <ESC>:call Run()<CR>
imap <F8> <ESC>:call ToHtml()<CR>
imap <F12> <ESC>:make<CR>

"------------------------------------------------------------
"file specific setting
"------------------------------------------------------------
"
	"C/C++
	"--------------------------------------------------------

	"Python
	"--------------------------------------------------------
	"pylint:
	"let g:pylint_onwrite = 0
	"autocmd FileType python compiler pylint  
	"pydict:
	"autocmd FileType python let g:pydiction_menu_height = 10
	"autocmd FileType python set complete+=k$VIM/complete-dict iskeyword+=.,(
	"exec python:
	"function! Python_Eval_VSplit() range
	"	let src = tempname()
	"	let dst = tempname()
	"	execute ": " . a:firstline . "," . a:lastline . "w " . src
	"	execute ":!python " . src . " > " . dst
	"	execute ":pedit! " . dst
	"endfunction
	"autocmd FileType python vmap <F2> :call Python_Eval_VSplit()<cr>
	autocmd FileType python setlocal et | setlocal sta | setlocal sw=4 | setlocal ts=4
	autocmd Filetype python set makeprg=python\ %
	autocmd FileType python set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
	
	"Mark down
	"--------------------------------------------------------
	autocmd BufRead,BufNewFile *.mkd setfiletype mkd

	"JSON
	"--------------------------------------------------------
	autocmd BufRead,BufNewFile *.json setfiletype json

"------------------------------------------------------------
"os specific setting
"-----------------------------------------------------------
if has("win32")
	"file encoding
	set fileencoding=GBK
	"auto complete dict location,s
	set tags+=$VIM/tags
	autocmd FileType python let g:pydiction_location = $VIM. "/complete-dict"
	"load/save of vimrc
	map <leader>s :source $VIM/_vimrc<cr>
	map <leader>e :e! $VIM/_vimrc<cr>
	autocmd! bufwritepost vimrc source $VIM/_vimrc
	"font settings
	if has("gui_running")
		set guifont=Droid\ Sans\ Mono:h10,Monaco:h10,Courier:h10
	endif
else
	"file encoding
	set fileencoding=UTF-8
	"auto complete dict location
	set tags+=~/.vim/tags
	autocmd FileType python let g:pydiction_location = "~/.vim/complete-dict"
	"load/save of vimrc
	map <leader>s :source ~/.vimrc<cr>
	map <leader>e :e! ~/.vimrc<cr>
	autocmd! bufwritepost vimrc source ~/.vimrc
	"font settings
	if has("gui_running")
		set guifont=Droid\ Sans\ Mono\ 10,Monaco\ 10,Monospace\ 10
	endif
endif

"------------------------------------------------------------
"functions 
"------------------------------------------------------------
func! Comp() 
	exec "w" 
	if &filetype == 'cpp'
		exec "!g++ % -g -o ".fnameescape("Debug/".expand("%<"))
	elseif &filetype == 'c' 
		exec "!gcc % -g -o ".fnameescape("Debug/".expand("%<"))
	elseif &filetype == 'python'
		exec "pylint"
	endif 
endfunc 

func! Run()
	exec "w"
	if &filetype == 'cpp' || &filetype == 'c'
		exec "!".fnameescape("Debug/".expand("%<"))
	elseif &filetype == 'python'
		exec "!python %"
	endif 
endfunc

func! Debug() 
	exec "w" 
	if &filetype == 'cpp' || &filetype == 'c'
		exec "!gdb ".fnameescape("Debug/".expand("%<")) 
	elseif &filetype == 'python'
		exec "!python -m pdb %"
	endif 
endfunc 

func! ToHtml()
    exec "set nonumber"	
	exec "let html_use_css=1"
	exec "let html_no_pre=1"
	exec "let use_xhtml=1"
	exec "TOhtml"
    exec "wq"
	exec "set number"
endfunc

